/*
 * Class Barrier 
 * Author : Rania Nader
 * Date of Creation : 25/03/2011
 * Source : http://programmingexamples.wikidot.com/java-barrier
 */


/*
 * Given object A, no more than one thread can execute a synchronized
   method of A at any time.
 */
public class Barrier
{
	static int c ;
    public synchronized void block() throws InterruptedException
    {
    	//System.out.println("in barrier");
    	if(c ==	2)
    	{
    		c=0;
    		System.out.println("counter is now 0");
    		releaseAll();
    	}    	
    	
    	else {
    		c++;
    		wait();
    	}
    }
 
    public synchronized void release() throws InterruptedException
    {
        notify();
    }
 
    public synchronized void releaseAll() throws InterruptedException
    {
        notifyAll();
    }
 
}